<?php

namespace Lims\DebtorBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Utils\SecurityBundle\CustomClass\SecurityFunctions;

class DebtorController extends Controller
{
    public function listAction()
    {
        $debtorList = $this->getDoctrine()->getManager()->getRepository('LimsMainBundle:Debtor')->findAllDebtor();        
        return $this->render('LimsDebtorBundle:Debtor:list.html.twig', array('debtorList' => $debtorList));
    }
    
    public function findByAjaxAction()
    {
        $registerNumber = SecurityFunctions::clear($_GET['q']); 
//        $registerNumber = SecurityFunctions::clear($_POST['registerNumber']);        
        
        $debtorList = $this->getDoctrine()->getManager()->getRepository('LimsMainBundle:Debtor')->findByAjax($registerNumber);
//        { results: [ {id:'CA', text:'California'}, {id:'CO', text: 'Colarado' ], more: false }
        $new = array();
        if($debtorList){
            foreach ($debtorList AS $k => $row)
            {
                $new[$k]['id'] = $row['REGISTER_NUMBER'];
                $new[$k]['text'] = $row['REGISTER_NUMBER'];
            }
        }        
        
        $response = new Response(json_encode($new));
        $response->headers->set('Content-Type', 'application/json');
        return $response;
        
    }
    
    
    
    
    
}
